package com.xxl.controller.impl;

import com.xxl.controller.BlogCommentApi;
import com.xxl.dto.BlogCommentDto;
import com.xxl.dto.BlogDto;
import com.xxl.dto.HomeDto;
import com.xxl.dto.vo.BlogCommentVo;
import com.xxl.service.BlogService;
import com.xxl.util.GlobalResult;
import com.xxl.util.JwtUtil;
import com.xxl.util.ModelData;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 实现BlogCommentApi接口
 * @author xxl
 * @date 2023/4/6
 */
@RestController
@Order(20)
@RequestMapping(value = "blogContent",produces = "application/json")
public class BlogCommentApiImpl implements BlogCommentApi<Map<String,Object>> {

    @Resource
    BlogService blogService;
    @Resource
    JwtUtil jwtUtil;
    @Resource
    GlobalResult resultUtil;
    @Resource
    ModelData modelData;




    @Override
    @GetMapping("like/{uuid}")
    public Map<String, Object> likeBlog(@PathVariable("uuid") String uuid) {
        //加载点赞文件
        modelData.readLikeFileTxt();

        HomeDto homeDto = blogService.queryBlogByUUID(uuid);
        if (homeDto == null) {
            return resultUtil.result(500,"点赞失败，博客不存在");
        }

        //如果包含这个uuid表示已经点过赞了
        if (modelData.like.contains(uuid)) {
            return resultUtil.result(500,"点赞失败，您已经点过赞了");
        }

        //这个的bug在于一个人可能同时点击多次，前端的带点赞应该是checkbox形式，后端也需要记录
        BlogDto blogDto = new BlogDto();
        blogDto.setStars(homeDto.getDto().getStars() + 1);
        blogService.modifyBlogByUUID(uuid,blogDto);

        //点赞成功就收录一个点赞，表示每个人只能点赞一次,并且写入文件
        modelData.like.add(uuid);
        modelData.saveLikeList();
        return resultUtil.result(200,"点赞成功");
    }

    @Override
    @GetMapping("cancelLike/{uuid}")
    public Map<String, Object> cancelLikeBlog(@PathVariable("uuid")String uuid) throws IOException {
        //加载点赞文件
        modelData.readLikeFileTxt();

        HomeDto homeDto = blogService.queryBlogByUUID(uuid);
        if (homeDto == null) {
            return resultUtil.result(500,"取消点赞失败，博客不存在");
        }

        //如果不包含这个uuid表示没有点赞了
        if (!modelData.like.contains(uuid)) {
            return resultUtil.result(500,"取消点赞失败，您没有点赞了");
        }

        //这个的bug在于一个人可能同时点击多次，前端的带点赞应该是checkbox形式，后端也需要记录
        BlogDto blogDto = new BlogDto();
        int stars = homeDto.getDto().getStars();
        blogDto.setStars(stars > 0 ? stars - 1 : 0);
        blogService.modifyBlogByUUID(uuid,blogDto);

        //若取消成功，则集合去掉这个uuid,并且更新文件
        modelData.like.remove(uuid);
        modelData.saveLikeList();
        return resultUtil.result(200,"取消点赞成功");
    }

    @Override
    @PostMapping("addComment")
    public Map<String, Object> comment(@RequestBody BlogCommentVo commentVo) {
        int i = blogService.postComment(commentVo);
        if (i > 0) {
            return resultUtil.result(200,"评论成功");
        }
        return resultUtil.result(500,"评论失败");
    }

    @Override
    @GetMapping("delete/{commentID}")
    public Map<String, Object> deleteComment(@PathVariable("commentID") String commentID,@RequestParam("uuid") String uuid) {

        Object[] objects = ableDelete(commentID, uuid);
        if (objects != null) {
            int code = (int) objects[0];
            String message = (String) objects[1];
            return resultUtil.result(code,message);
        }

        int i = blogService.deleteComment(commentID);

        if (i > 0) {
            return resultUtil.result(200,"删除评论成功");
        }


        return resultUtil.result(500,"删除评论失败");
    }

    @Override
    @GetMapping("deleteSelfBlogComment/{commentID}")
    public Map<String, Object> deleteSelfBlogComment(@PathVariable("commentID")String commentID,@RequestParam("uuid") String uuid) {

        Object[] objects = ableSelfDelete(commentID, uuid);
        if (objects != null) {
            int code = (int) objects[0];
            String message = (String) objects[1];
            return resultUtil.result(code,message);
        }
        int i = blogService.deleteComment(postCommentUsername,commentID);

        if (i > 0) {
            return resultUtil.result(200,"删除评论成功");
        }
        return resultUtil.result(500, "删除评论失败");
    }

    @Override
    @PostMapping("noLike/{uuid}")
    public Map<String, Object> noLikeBlog(@PathVariable("uuid") String uuid) {
        HomeDto homeDto = blogService.queryBlogByUUID(uuid);
        if (homeDto == null) {
            return resultUtil.result(500,"踩失败，博客不存在");
        }
        BlogDto blogDto = new BlogDto();
        blogDto.setDisagreeable(homeDto.getDto().getDisagreeable() + 1);
        blogService.modifyBlogByUUID(uuid,blogDto);

        return resultUtil.result(500,"踩成功");
    }

    @Override
    @PostMapping("cancelNoLike/{uuid}")
    public Map<String, Object> cancelNoLikeBlog(@PathVariable("uuid") String uuid) {
        HomeDto homeDto = blogService.queryBlogByUUID(uuid);
        if (homeDto == null) {
            return resultUtil.result(500,"取消踩失败，博客不存在");
        }
        BlogDto blogDto = new BlogDto();
        int disagreeable = homeDto.getDto().getDisagreeable();
        blogDto.setDisagreeable(disagreeable > 0 ? disagreeable - 1: 0);
        blogService.modifyBlogByUUID(uuid,blogDto);
        return resultUtil.result(200,"取消踩成功");
    }

    /**
     * 品论的主人是否能删除
     * 第0下标是状态码  1下标是提示  null表撒施可以删除
     * 1、先查询是否有这样的博客
     * 2、查询博客的所有评论中是否有当前用户的名字
     */
    public Object[] ableDelete(String commentId,String uuid) {
        Object[] objects = new Object[2];
        List<BlogCommentDto> commentList = blogService.queryAllCommentByUUID(uuid);
        if (commentList == null) {
            objects[0] = 500;
            objects[1] = "删除失败，没有"+uuid+"的博客";
            return  objects;
        }
        //查询博客的评论集合中是否有当前用户的用字
        List<BlogCommentDto> list = commentList
                .stream()
                .filter(dto -> dto.getUsername().equals(jwtUtil.getTokenUser().getUsername()))
                .toList();
        if (list.size() == 0) {
            objects[0] = 500;
            objects[1] = "删除失败，当前用户没有评论" + uuid + "的博客";
            return  objects;
        }

        List<BlogCommentDto> blogCommentDtos = list.stream().filter(dto -> dto.getId().equals(commentId)).toList();
        if (blogCommentDtos.size() == 0) {
            objects[0] = 500;
            objects[1] = "删除失败，你并没有" + commentId + "的评论去评论过博客"+uuid;
            return  objects;
        }
        return null;
    }

    /**
     * 评论的主人删除评论
     */
    public String postCommentUsername = null;
    public Object[] ableSelfDelete(String commentId,String uuid) {
        Object[] objects = new Object[2];
        List<BlogCommentDto> commentList = blogService.queryAllCommentByUUID(uuid);
        if (commentList == null) {
            objects[0] = 500;
            objects[1] = "删除失败，没有"+uuid+"的博客";
            return  objects;
        }
        String blogMasterName = blogService.queryBlogByUUID(uuid).getDto().getUsername();
        //这个uuid的博客的原有主人和删除的人相等时则表示可以是删除
        if (!blogMasterName.equals(jwtUtil.getTokenUser().getUsername())) {
            objects[0] = 500;
            objects[1] = "删除失败，你不是"+uuid+"的博客的主人";
            return  objects;
        }
        List<String> list = commentList
                .stream()
                .filter(dto -> dto.getId().equals(commentId))
                .map(dto -> dto.getUsername())
                .toList();
        if (list.size() == 0) {
            objects[0] = 500;
            objects[1] = "删除失败，没有" + commentId + "的评论";
            return  objects;
        }
        postCommentUsername = list.get(0);
        return null;
    }
}
